﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public partial class TabControl
    {

        /// <summary>
        /// 获取该选项卡控件中选项卡页的集合。
        /// </summary>
        /// <returns></returns>
        [Category("行为")]
        [Description("获取该选项卡控件中选项卡页的集合。")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new TabPageCollection TabPages
        {
            get
            {
                if (this.m_TabPages is null)
                {
                    this.m_TabPages = new TabPageCollection(this);
                }

                return this.m_TabPages;
            }
        }

        private TabPageCollection m_TabPages;

        [DefaultValue(typeof(Font), "微软雅黑, 9pt")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }

            set
            {
                base.Font = value;
            }
        }

        /// <summary>
        /// 获取或设置选项卡在其中对齐的控件区域（例如，顶部边缘）。
        /// </summary>
        /// <returns>System.Windows.Forms.TabAlignment 值之一。 默认值为 Top。</returns>
        [DefaultValue(TabAlignment.Top)]
        [RefreshProperties(RefreshProperties.All)]
        [Category("行为")]
        [Description("获取或设置选项卡在其中对齐的控件区域（例如，顶部边缘）。")]
        public TabAlignment Alignment { get; set; } = TabAlignment.Top;

        /// <summary>
        /// 获取或设置在控件的选项卡上显示的图像。
        /// </summary>
        /// <returns>System.Windows.Forms.ImageList，指定在选项卡上显示的图像。</returns>
        [DefaultValue(null)]
        public ImageList ImageList
        {
            get
            {
                return this.m_ImageList;
            }

            set
            {
                this.m_ImageList = value;
                foreach (TabPage tabPage in this.TabPages)
                    tabPage.ImageList = value;
            }
        }

        private ImageList m_ImageList;

        /// <summary>
        /// 获取该选项卡控件的标签处的间距。
        /// </summary>
        /// <returns></returns>
        [Browsable(true)]
        [DefaultValue(typeof(Point), "0,50")]
        [Category("行为")]
        [Description("获取该选项卡控件的标签处的间距。")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public new Point Padding
        {
            get
            {
                return this.m_padding;
            }

            set
            {
                this.m_padding = value;
                base.Padding = new Padding(0, value.Y, 0, 0);
                this.Invalidate();
            }
        }

        private Point m_padding = new Point(0, 50);

        // Private m_SelectedIndex As Integer = -1
        // ''' <summary>
        // ''' 获取或设置当前选定的选项卡页的索引。
        // ''' </summary>
        // ''' <returns>当前选定的选项卡页的从零开始的索引。 默认值为 -1，当未选定任何选项卡页时也为此值。</returns>
        // <Browsable(False)>
        // <DefaultValue(-1)>
        // Public Property SelectedIndex As Integer
        // Get
        // Return m_SelectedIndex
        // End Get
        // Set(ByVal value As Integer)
        // If m_SelectedIndex <> value Then
        // m_SelectedIndex = value
        // Me.SelectedTab = Me.TabPages(value)
        // End If
        // End Set
        // End Property

        private TabPage m_SelectedTab;

        /// <summary>
        /// 获取或设置当前选定的选项卡页。
        /// </summary>
        /// <returns></returns>
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("行为")]
        [Description("获取或设置当前选定的选项卡页。")]
        public TabPage SelectedTab
        {
            get
            {
                return this.m_SelectedTab;
            }

            set
            {
                if (!Equals(this.m_SelectedTab, value))
                {
                    try
                    {
                        var tabPageChanging = new TabPageChangingEventArgs(this.m_SelectedTab, value);
                        SelectedTabChanging?.Invoke(this, tabPageChanging);
                        if (!tabPageChanging.Cancel)
                        {
                            if (value is object && this.TabPages.Contains(value))
                            {
                                value.Visible = true;
                                value.BringToFront();
                            }

                            if (this.m_SelectedTab is object)
                            {
                                this.m_SelectedTab.Visible = false;
                            }

                            this.m_SelectedTab = value;
                            var tabPageChanged = new TabPageChangedEventArgs(this.m_SelectedTab, value);
                            SelectedTabChanged?.Invoke(this, tabPageChanging);
                        }
                    }
                    finally
                    {
                        this.Invalidate();
                    }
                }
            }
        }

        private bool m_showTabs = true;
        /// <summary>
        /// 获取或设置是否显示选项卡的标题。
        /// </summary>
        /// <returns></returns>
        [DefaultValue(true)]
        [Category("行为")]
        [Description("获取或设置是否显示选项卡的标题。")]
        public bool ShowTabs
        {
            get
            {
                return this.m_showTabs;
            }

            set
            {
                this.m_showTabs = value;
                base.Padding = new Padding(0, value ? 50 : 0, 0, 0);
                this.Invalidate();
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ControlCollection Controls
        {
            get
            {
                return base.Controls;
            }
        }

        /// <summary>
        /// 获取或设置控件的背景色。
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        [DefaultValue(typeof(Color), "White")]
        public new Color BackColor
        {
            get
            {
                return this.m_BackColor;
            }

            set
            {
                this.m_BackColor = value;
                base.BackColor = value;
            }
        }

        private Color m_BackColor = Color.White;

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Cursor Cursor
        {
            get
            {
                return base.Cursor;
            }

            set
            {
                base.Cursor = value;
            }
        }
    }
}